import numpy as np
import cv2

# 加载原始图像
image = cv2.imread('piano512.PNG', cv2.IMREAD_GRAYSCALE)

# 加载水印图像
watermark = cv2.imread('lsc_sign512.jpg', cv2.IMREAD_GRAYSCALE)

# 执行SVD
U, S, V = np.linalg.svd(image, full_matrices=False)

# 嵌入水印
alpha = 0.3  # 调整水印强度
watermarked_image = U @ np.diag(S + alpha * watermark) @ V

# 显示原始图像、水印和水印后的图像
cv2.imshow('Original Image', image)
cv2.imshow('Watermark', watermark)
cv2.imshow('Watermarked Image', watermarked_image.astype(np.uint8))
cv2.waitKey(0)
cv2.destroyAllWindows()

# 提取水印
extracted_watermark = (np.diag(S + alpha * watermark) - S) / alpha

# 显示提取的水印
cv2.imshow('Extracted Watermark', extracted_watermark.astype(np.uint8))
cv2.waitKey(0)
cv2.destroyAllWindows()
